const WXAPI = require('../../utils/main')
const util = require('../../utils/util');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    reasons: [
      "请选择",
      "质量问题", 
      "商品变质", 
      "卖家发错货",
      "拍错了",
      "商品与描述不符"
      ],
    reasonIndex: 0,//原因序号
    files: [],//原因
    obtainPrice:'',//固定金额
    price:'',//变动金额
    disabled:true,//修改金额
    explain:'',//退款说明
    result:'',//商品信息
    content:'必填,请您详细填写申请说明'
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let result = JSON.parse(options.result)
    this.setData({
      result:result,
      price:result.gprice,
      obtainPrice:result.gprice
    })
  },
  //退款原因
  reasonChange: function (e) {
    this.setData({
      reasonIndex: e.detail.value
    })
  },
  //退款金额
  getIntegral:function(e) {
    console.log(e)
    let that = this
    let obtainPrice = that.data.obtainPrice
    util.formatNum(e)
    if(e.detail.value > obtainPrice) { 
      wx.showToast({
        title: '不能超过退款金额',
        icon: 'none',
      })
      that.setData({
        price: obtainPrice,
      })
    }else {
      that.setData({
        price: e.detail.value,
      })
    }
  },
  //修改金额
  modifyPrice:function(e){
    console.log(e)
    // this.setData({
    //   disabled:false
    // })
    wx.showToast({
      title: '此功能暂未开放',
      icon: 'none',
    })
  },


  apply_btn:function(){
    let that = this
    //用于保存在数据库中的地址数据
    let refund_data = {};
    //构造数据验证表单是否存在空值
    let data = {
      reasonIndex: {
        value: '',
        msg: '请选择原因'
      },
      explain: {
        value: '',
        msg: '请简述退款说明'
      },
      files: {
        value: '',
        msg: '请上传照片'
      },
    };
    for (let key in data) {
      if (this.data[key] == data[key].value || this.data.reasonIndex == 0) {
        wx.showToast({
          title: data[key].msg,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      refund_data[key] = this.data[key];
    }
    console.log(that.data.id)

    let files = that.data.files
    console.log(files)
    for (let i = 0; i< files.length; i++) {
      wx.uploadFile({
        url: 'https://zmj.aneasyhome.com/xiaochengxu/d_order.php', 
        filePath: files[i],
        name: 'file',
        formData:{
          _id:that.data.result.orderNmberId,
          _uid:wx.getStorageSync('uid'),
          _refund:1,
          _remark:that.data.reasons[refund_data.reasonIndex],
          _reason:refund_data.explain,
        },
        success: function(res){
          console.log(res)
          wx.showToast({
            title: '提交申请成功,请前往退款/售后查看',
            icon: 'none',
          })
          setTimeout(() => {
            that.changeParentData()
          },2000)
        }
      })
    }
  },
  //返回上一级页面
  changeParentData: function () {
    let pages =getCurrentPages();//当前页面栈
    if (pages.length >1) {
        let beforePage = pages[pages.length- 2];//获取上一个页面实例对象
        beforePage.changeData();//触发父页面中的方法
        wx.navigateBack()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //组件返回参数
  signEventt: function(e){
    let that = this
    that.setData({
      explain:e.detail.explain
    })
  },
  signEvent: function(e){
    let that = this
    that.setData({
      files:e.detail.files
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})